Skip to content

Fix Product::setConsiderStock() - must be of type bool, string given - #24

Open
Marco (marcoherzog) wants to merge 1 commit into
jtl-software:developfrom
marcoherzog:master
Open

Fix Product::setConsiderStock() - must be of type bool, string given#24
Marco (marcoherzog) wants to merge 1 commit into
jtl-software:developfrom
marcoherzog:master

Conversation

@marcoherzog

Copy link
Copy Markdown

In woo-jtl-connector/vendor/jtl/connector/src/Model/Product.php the method setConsiderStock expects its argument to be boolean. But the return value from WooCommerce can actually be mixed. In this case the string 'yes' or 'no'. The patch converts 'yes' to boolean true.

In woo-jtl-connector/vendor/jtl/connector/src/Model/Product.php the method setConsiderStock expects its argument to be boolean. But the return value from WooCommerce can actually be mixed. In this case the string 'yes' or 'no'. The patch converts 'yes' to boolean true.
@marcoherzog

Copy link
Copy Markdown
Author

Exception: Jtl\Connector\Core\Model\Product::setConsiderStock(): Argument #1 ($considerStock) must be of type bool, string given, called in /var/www/vhosts/carubina.com/wp/production/releases/118/web/app/plugins/woo-jtl-connector/src/Controllers/ProductController.php on line 135

Problem

function get_prop returns mixed ('yes' or 'no' string) while function setConsiderStock expects boolean.

Trace

/woo-jtl-connector/src/Controllers/ProductController.php

->setConsiderStock($product->managing_stock())

/woo-jtl-connector/vendor/jtl/connector/src/Model/Product.php

function setConsiderStock expects $considerStock to be boolean.

/**
 * @param bool $considerStock Consider stock level?
 *                            If true, product can only be purchased with a positive stockLevel or when
 *                            permitNegativeStock is set to true
 *
 * @return $this
 */
public function setConsiderStock(**bool $considerStock**): self
{
    $this->considerStock = $considerStock;

    return $this;
}

/woocommerce/includes/abstracts/abstract-wc-product.php

/**
 * Returns whether or not the product is stock managed.
 *
 * @return bool
 */
public function managing_stock() {
	if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
		return $this->get_manage_stock();
	}
	return false;
}

/woocommerce/includes/abstracts/abstract-wc-product.php

/**
 * Return if product manage stock.
 *
 * @since  3.0.0
 * @param  string $context What the value is for. Valid values are view and edit.
 * @return boolean
 */
public function get_manage_stock( $context = 'view' ) {
	return $this->get_prop( 'manage_stock', $context );
}

web/app/plugins/woocommerce/includes/abstracts/abstract-wc-data.php

get_prop can return mixed.

/**
 * Gets a prop for a getter method.
 *
 * Gets the value from either current pending changes, or the data itself.
 * Context controls what happens to the value before it's returned.
 *
 * @since  3.0.0
 * @param  string $prop Name of prop to get.
 * @param  string $context What the value is for. Valid values are view and edit.
 * @return mixed
 */
protected function get_prop( $prop, $context = 'view' ) {
	$value = null;

	if ( array_key_exists( $prop, $this->data ) ) {
		$value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ];

		if ( 'view' === $context ) {
			$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
		}
	}

	return $value;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant